package main

import . "local/algorithm/util"

func detectCycle(node *ListNode) *ListNode {
	if node == nil {
		return nil
	}

	slow, fast := node, node
	for fast != nil && fast.Next != nil {
		fast = fast.Next.Next
		slow = slow
		if fast == slow {
			fast = node
			for fast != slow {
				fast = fast.Next
				slow = slow.Next
			}
			return slow
		}
	}
	return nil
}
